#include "selectscene.h"
#include <QPushButton>
#include <QPainter>
#include <QMenuBar>
#include "mypushbutton.h"
#include "playscene.h"
selectScene::selectScene(QWidget *parent)
    : myMainWindow{parent}
{
    //返回按钮
    MyPushButton *btnBack = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png",this);
    btnBack->resize(72,32);
    connect(btnBack,&QPushButton::clicked,this,&selectScene::backBtnClicked);
    //移动按钮
    btnBack->move(this->width()-btnBack->width(),this->height()-btnBack->height());


    //关卡按钮20个
    const int colWidth = 70;
    const int rowHeight = 70;

    //x y 偏移
    const int xOffset = 25;
    const int yOffset = 130;

    for(int i = 0; i<20; ++i)
    {
        MyPushButton *btn = new MyPushButton(":/res/LevelIcon.png",":/res/LevelIcon.png",this);
        btn->setText(QString::number(i+1));
        //排列
        //行 i / 4
        //列 i % 4
        int row  = i / 4;
        int col = i % 4;
        // x 坐标 = 列数 * 列宽 + x偏移
        // y 坐标 = 行数 * 列高 + y偏移
        int  x = col * colWidth + xOffset;

        int y = row * rowHeight + yOffset;

        btn->resize(57,57);
        btn->move(x,y);

        connect(btn,&MyPushButton::clicked,[=]{
            //点击关卡按钮后打开一个新的场景PlayScene
            PlayScene *playScene = new PlayScene(i+1);
            playScene->setAttribute(Qt::WA_DeleteOnClose);
            playScene->move(this->pos());
            playScene->show();
            this->hide();

            connect(playScene,&PlayScene::backBtnClicked,[=]{
                this->move(playScene->pos());
                this->show();
                playScene->close();

            });
        });
    }
}

void selectScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    //搬动画家到菜单栏下面
    painter.translate(0,this->menuBar()->height());
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    pix.load(":/res/Title.png");
    painter.drawPixmap(0,0,pix);

}
